package opencv.service;


import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

/**
 * @author Aakash Polra
 */
public class SimpleDebugGUI extends Panel {
	
	private static final long serialVersionUID = 1L;
	
	protected Image currentDebugFrame;
	protected JPanel debugFramePanel;
	
	protected Application application;
	
	public SimpleDebugGUI() {
		initApplication();
		initializeComponents();
		
		// Try to load libraries
//		try {
//			File openCVLibraries = new File("C:/OpenCV2.0/bin"); //"lib/opencv/");
//			if (openCVLibraries.exists() && openCVLibraries.isDirectory()) {
//				File[] libFiles = openCVLibraries.listFiles();
//				
//				for (int i=0; i < libFiles.length; i++) {
//					if (libFiles[i].getName().toLowerCase().endsWith("dll")) {
//						System.load(libFiles[i].getAbsolutePath());
//						System.out.println("Library file: " + libFiles[i].getName() + " loaded.");
//					}
//				}
//			}
//			else
//			{
//				System.out.println("OpenCV libraries not found in folder lib/opencv/");
//			}
//		}
//		catch(Exception e) {
//			e.printStackTrace();
//		}
	}
	
	protected void initApplication() {
		this.application = new Application();
	}
	
	public void showDebug(Image image) {
		currentDebugFrame = image;
		debugFramePanel.repaint();
	}
	
	protected void startApp() {
		this.application.start();
	}
	
	protected void stopApp() {
		this.application.stop();
	}
	
	protected void onShowDebugChanged(boolean newValue) {
		
	}
	
	protected void initializeComponents() {
		
		//
		final JToggleButton btnShowDebug = new JToggleButton("Show debug output");
		btnShowDebug.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				onShowDebugChanged(btnShowDebug.isSelected());
			}
		});
		
		debugFramePanel = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g) {
				super.paintComponents(g);
				g.drawImage(currentDebugFrame, 0, 0, null);
			};
		};
		debugFramePanel.setSize(640, 480);
		debugFramePanel.setPreferredSize(new Dimension(640, 480));
		
//		setLayout(new GridLayout(3, 1));
		
		
		//-------------------------------------
		final JToggleButton btnRun = new JToggleButton("Run");
		btnRun.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (btnRun.isSelected()) {
					startApp();
					btnRun.setText("Running...");
				} else {
					stopApp();
					btnRun.setText("Run");
				}
			}
		});
		
		// Add components
		JPanel panel1 = new JPanel();
		panel1.add(btnShowDebug);
		panel1.setSize(100, 40);
		JPanel panel2 = new JPanel();
		panel2.add(btnRun);
		
		add(panel1);
		add(debugFramePanel);
		add(panel2);
	}

	public void showFrame() {
		JFrame frame = new JFrame();
		frame.getContentPane().add(this);
		frame.pack();
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				stopApp();
//				while(application.isRunning()) Thread.yield();	// spin until the application is stopped fully
				System.exit(0);
			}
		});
	}
	
	public Application getApplication() {
		return application;
	}
	
	public static void main(String[] args) throws AWTException {
		new SimpleDebugGUI().showFrame();
	}

}
